# -*- coding: utf-8 -*-
"""
@author: luhx
@file: strategy_top.py
@time: 2023/7/4 15:37
@desc: 自动平仓逻辑
"""
import sys
import traceback

from kds_util.user_logbook import init_logger as init_logs, user_log as logger
from highlow import algo
from highlow.dd_bkinfo import DdBkInfo
from highlow.envs import Envs
from highlow import envs
from highlow.strategy_auto_close_position_imp import StrategyAutoClosePositionImp
from typing import Callable
from kds_util.json_config_mgr import ConfigMgr
from base import base_util

def main_kbase(n_days=220, n_minute_days=2, stime="", etime="", codes=[]):
    xj = StrategyAutoClosePositionImp(Envs.investor_id, n_days, n_minute_days, stime=stime, etime=etime, codes=codes)
    xj.runallklines()
    return


if __name__ == '__main__':
    try:
        if len(sys.argv) < 2:
            raise "参数len(sys.argv) < 2, 必须提供investor_id"
        Envs.investor_id = sys.argv[1]
        init_logs(path="./log", name=f"strategy_{Envs.investor_id}", level="INFO")
        envs.init_envs(Envs.investor_id)
        Envs.dest_ip = base_util.parse_ip_from_url(Envs.dest_url)
        pdays = DdBkInfo.init_ths_trading_day(Envs.dest_ip)
        if Envs.dest_play_back:
            trading_days = algo.init_trade_day(pdays, Envs.dest_play_start_time)
            start_time = Envs.dest_play_start_time
            end_time = Envs.dest_play_end_time
        else:
            start_time = ""
            end_time = ""
            trading_days = algo.init_trade_day(pdays, start_time)

        main_kbase(n_days=100, n_minute_days=2, stime=start_time,
                   etime=end_time, codes=Envs.dest_codes)

    except Exception as ex:
        logger.error(str(ex))
        logger.error(traceback.format_exc())
